package com.amsure.utils;
import com.amsure.entity.BaseCommonEntity;
import org.apache.commons.lang.StringEscapeUtils;
import org.json.JSONObject;
import java.util.HashMap;
import static com.amsure.utils.CommonUtils.getMapToString;
import static com.amsure.utils.CommonUtils.getStringToMap;
import static com.holley.sdk.emcpshare.HMacMD5.getHmacMd5Str;
import static com.holley.sdk.emcpshare.ShareSecurityUtil.aesDecrypt;
import static com.holley.sdk.emcpshare.ShareSecurityUtil.aesEncrypt;

/**
 * @date 2021/8/21
 * @ddescription:
 */
public class EncodeUtils {
    /**
     * @Description: 发送的5项信息转换成string
     * @Param:
     * @return:
     */
    public static String sendPostMaptoString(BaseCommonEntity sendPostBaseCommonEntity) throws Exception {
        HashMap<String, Object> objectObjectHashMapPost = new HashMap<>(5);
        objectObjectHashMapPost.put("\"operatorId\"", "\"" + sendPostBaseCommonEntity.getOperatorId() + "\"");
        objectObjectHashMapPost.put("\"data\"", "\"" + sendPostBaseCommonEntity.getData() + "\"");
        objectObjectHashMapPost.put("\"seq\"", "\"" + sendPostBaseCommonEntity.getSeq() + "\"");
        objectObjectHashMapPost.put("\"sig\"", "\"" + sendPostBaseCommonEntity.getSig() + "\"");
        objectObjectHashMapPost.put("\"timeStamp\"", "\"" + sendPostBaseCommonEntity.getTimeStamp() + "\"");
        String stringPostTemporary = getMapToString(objectObjectHashMapPost);
        return "{" + stringPostTemporary + "}";
    }

    /**
     * @Description: 输入需要的信息，加密
     * @Param: operatorId:运营商编码
     * @Param: operatorSecret:商户密钥
     * @Param: seq:固定4位自增序列:0001
     * @Param: timeStamp:时间戳: yyyyMMddHHmmss
     * @Param: sig:签名
     * @Param: dataSecret:消息密钥
     * @Param: dataSecretIV:消息密钥初始化向量
     * @return:
     */
    public static BaseCommonEntity encodeDataForToken(String operatorId, String operatorSecret, String seq, String timeStamp, String
            dataSecret, String dataSecretIV, String sigSecret) throws Exception {
        //operatorId和operatorSecret加密成data
        String data = "{\"operatorId\":\"" + operatorId + "\",\"operatorSecret\":\"" + operatorSecret + "\"}";
        JSONObject jsonObject = new JSONObject(data);
        String stringData = jsonObject.toString();
        String stringEncodeData = aesEncrypt(stringData, dataSecret, dataSecretIV);
        //加密sig
        String stringCodePost = operatorId + stringEncodeData + timeStamp + seq;
        String stringEncodeSig = getHmacMd5Str(sigSecret, stringCodePost);
        BaseCommonEntity sendPostEntity = new BaseCommonEntity();
        sendPostEntity.setOperatorId(operatorId);
        sendPostEntity.setData(stringEncodeData);
        sendPostEntity.setSeq(seq);
        sendPostEntity.setTimeStamp(timeStamp);
        sendPostEntity.setSig(stringEncodeSig);
        sendPostEntity.setDataSecret(dataSecret);
        sendPostEntity.setDataSecretIV(dataSecretIV);
        return sendPostEntity;
    }

    /**
     * @Description: 发送信息后返回来的信息，获得其中的一项
     * @Param:
     * @return:
     */
    public static String getPartFromAll(String receiveInformation, String stringTakeFromInformation) {
        if (receiveInformation != null) {
            String receiveInformationTemporary = receiveInformation.substring(1, receiveInformation.length() - 1);
            HashMap<String, Object> stringToMapReceiveInformation = getStringToMap(receiveInformationTemporary);
            return stringToMapReceiveInformation.get(stringTakeFromInformation).toString();
        }
        return null;
    }

    /**
     * @Description: 解密
     * @Param:
     * @return: 解密string
     */
    public static String decodeInformation(String stringEncode, BaseCommonEntity baseCommonEntity) throws
            Exception {
        String stringEncodeTokenWithEscapeCharacterTemporary = StringEscapeUtils.unescapeJava(stringEncode);
        String stringEncodeTokenWithEscapeCharacter = stringEncodeTokenWithEscapeCharacterTemporary.substring(1, stringEncodeTokenWithEscapeCharacterTemporary.length() - 1);
        return aesDecrypt(stringEncodeTokenWithEscapeCharacter, baseCommonEntity.getDataSecret(), baseCommonEntity.getDataSecretIV());
    }

    /**
     * @Description: 加密账户充值data sig加密
     * @Param: userId  tradeNo   money
     * @return: baseAccountChargeCommonEntity
     */
    public static BaseCommonEntity encodeDataForAccountCharge(BaseCommonEntity baseAccountChargeCommonEntity) throws Exception {
        //加密data
        String data = "{\"userId\":\"" + baseAccountChargeCommonEntity.getUserId() + "\",\"tradeNo\":\"" +
                baseAccountChargeCommonEntity.getTradeNo() + "\"," + "\"money\":\"" + baseAccountChargeCommonEntity.getMoney() + "\"}";
        JSONObject jsonObjectAccountCharge = new JSONObject(data);
        String stringDataAccountCharge = jsonObjectAccountCharge.toString();
        String stringEncodeDataAccountCharge = aesEncrypt(stringDataAccountCharge, baseAccountChargeCommonEntity.getDataSecret
                (), baseAccountChargeCommonEntity.getDataSecretIV());
        baseAccountChargeCommonEntity.setData(stringEncodeDataAccountCharge);
        //加密sig
        String stringSigAccountCharge = baseAccountChargeCommonEntity.getOperatorId() + baseAccountChargeCommonEntity.getData() +
                baseAccountChargeCommonEntity.getTimeStamp() + baseAccountChargeCommonEntity.getSeq();
        String stringEncodeSigAccountCharge = getHmacMd5Str(baseAccountChargeCommonEntity.getSigSecret(), stringSigAccountCharge);
        baseAccountChargeCommonEntity.setSig(stringEncodeSigAccountCharge);
        return baseAccountChargeCommonEntity;
    }




}
